package com.cms.admin.action.stuff;

import com.aliyun.oss.model.PutObjectResult;
import com.cms.admin.action.BaseAction;
import com.cms.admin.model.*;
import com.cms.admin.service.common.DepartmentService;
import com.cms.admin.service.common.EntityService;
import com.cms.admin.service.privilege.UserListService;
import com.cms.admin.service.stuff.StuffService;
import com.cms.admin.util.Constants;
import com.cms.admin.util.MD5Encrypt;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * Created by YYY-Work on 2017/5/4.
 */
public class AddStuffAction extends BaseAction implements ModelDriven<StuffListExt>{

    public List<EntityList> entityLists;
    private List<DepartmentList> departmentLists;

    private EntityService entityService;
    private DepartmentService departmentListService;
    private StuffService stuffService;
    private UserListService userListService;

    private StuffListExt search=new StuffListExt();

    public String requestAddStuffList()
    {
        List<EntityList> entities=entityService.queryAllEntityList();
        List<DepartmentList> departments=departmentListService.getAllDepartmentList();
        setEntityLists(entities);
        setDepartmentLists(departments);
        return "success";
    }

    public String ajaxAddStuffList()
    {
        HttpServletRequest request= ServletActionContext.getRequest();
        initBathPath();
        if (search.getTypeid()==2)
        {
            search.setTypeName("客服");
            search.setDepartmentid(null);
            search.setStuffTitle(null);
            search.setStuffLabel(null);
            search.setEntityid(2);
        }else
        {
            search.setTypeName("医生");
        }

        if (null!=search.getRegisterWhere())
        {
            StringBuffer registertypeidBF=new StringBuffer();
            if (search.getRegisterWhere().contains("1")||search.getRegisterWhere().contains("2")||search.getRegisterWhere().contains("7"))
            {
                registertypeidBF.append("1,");
            }

            if (search.getRegisterWhere().contains("3"))
            {
                registertypeidBF.append("2,");
            }

            if (search.getRegisterWhere().contains("6"))
            {
                registertypeidBF.append("3,");
            }

            if (search.getRegisterWhere().contains("4"))
            {
                registertypeidBF.append("4,");
            }
            String registertypeid=registertypeidBF.toString();
            if (registertypeid.endsWith(","))
            {
                registertypeid=registertypeid.substring(0,registertypeid.length());
            }
            search.setRegistertypeid(registertypeid);
        }

        /**
         * 上传头像
         * */
        String iconUrl=null;
        if (null!=search.getIcoUrlImage())
        {
            String tgs=null;
            if (search.getEntityid()==2)
            {
                tgs="2";
            }else
            {
                tgs="3";
            }

            try {
                iconUrl=uploadStuffIcon(search.getIcoUrlImage(),tgs,search.getIcoUrlImageFileName());
            }catch (Exception e)
            {
                LOG.error("uploadStuffIcon",e);
            }
            iconUrl=null==iconUrl?"":iconUrl;
            search.setIconUrl(iconUrl);
        }


        UserList userList=userListService.getByNo(search.getStuffNo()+"#");
        if (null!=userList)
        {
            StuffList stuffList=stuffService.queryStuffListById(userList.getUserid());
            if (null==stuffList)
            {
                search.setOrderbycolumn((long) 100);
                search.setStuffid(userList.getUserid());
                stuffService.addStuff(search);
            }
            return "success";
        }else
        {
            userList=new UserList();
            userList.setUserNo(search.getStuffNo()+"#");
            userList.setUserName(search.getStuffName());
            userList.setPassword(MD5Encrypt.MD5Encode("123456"));
            userList.setPasswordCreateDate(new Date());
            userList.setTypeid(search.getTypeid());
            if (3==search.getTypeid())
            {
                userList.setTypeName("医生");
            }else
            {
                userList.setTypeName("客服");
            }
            userList.setRegisterDate(new Date());
            userList.setIsUsedFlag((short) 1);
            userList.setCreateDate(new Date());
            userList.setUpdateDate(new Date());
            userList.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
            search.setOrderbycolumn((long) 100);
            stuffService.addStuff(search,userList);
            return "success";

        }
    }


    private String uploadStuffIcon(File img, String tgs,String fileName)
    {
        //后缀
        try {
            String tailSuffix = getExtension(fileName);
            String DIR="docImage/"+tgs+"/"+System.currentTimeMillis()+tailSuffix;
            PutObjectResult result=uploadByOss(img,DIR);
            if (null!=result)
            {
                return DIR;
            }else
            {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    public List<EntityList> getEntityLists() {
        return entityLists;
    }

    public void setEntityLists(List<EntityList> entityLists) {
        this.entityLists = entityLists;
    }

    public List<DepartmentList> getDepartmentLists() {
        return departmentLists;
    }

    public void setDepartmentLists(List<DepartmentList> departmentLists) {
        this.departmentLists = departmentLists;
    }

    public EntityService getEntityService() {
        return entityService;
    }

    public void setEntityService(EntityService entityService) {
        this.entityService = entityService;
    }

    public DepartmentService getDepartmentListService() {
        return departmentListService;
    }

    public void setDepartmentListService(DepartmentService departmentListService) {
        this.departmentListService = departmentListService;
    }

    public StuffService getStuffService() {
        return stuffService;
    }

    public void setStuffService(StuffService stuffService) {
        this.stuffService = stuffService;
    }

    public UserListService getUserListService() {
        return userListService;
    }

    public void setUserListService(UserListService userListService) {
        this.userListService = userListService;
    }

    @Override
    public StuffListExt getModel() {
        return search;
    }

    public StuffListExt getSearch() {
        return search;
    }

    public void setSearch(StuffListExt search) {
        this.search = search;
    }
}
